<template>
  <j-modal title="谈话录入" :width="1100" :visible="visible" switchFullscreen :maskClosable="false"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" :destroyOnClose="true"
    :footer="null">
    <a-spin :spinning="confirmLoading">
      <div class="con-content">
        <div class="con-content-top">
          <a-steps :current="current" size="small">
            <a-step v-for="item in steps" :key="item.title" :title="item.title" />
          </a-steps>
        </div>
        <div class="content-form">
          <BaseInformation v-show="current === 0" ref="baseInformationId" @personChange="onPersonChange" />
          <TalkInformation v-show="current === 1" ref="talkInformationId" />
          <div class="footerBox">
            <a-button class="btn" @click="handleCancel">
              取消
            </a-button>
            <a-button v-if="current > 0" style="margin-left: 8px" @click="prev">
              上一步
            </a-button>
            <a-button v-if="current < steps.length - 1" style="margin-left: 8px" type="primary" @click="next">
              下一步
            </a-button>
            <!-- <a-popconfirm title="确定发起审批？" ok-text="是" cancel-text="否" @confirm="submitHandler()"> -->
            <a-button type="primary" @click="submitHandler" style="margin-left:8px;" v-if="current === 1"
              :loading="submitLoading">
              提交
            </a-button>
            <!-- </a-popconfirm> -->
          </div>
        </div>
      </div>
    </a-spin>
  </j-modal>
</template>

<script>
import * as educationTalk from '@/api/sjsk/help/educationTalk.js'
import BaseInformation from './BaseInformation'
import TalkInformation from './TalkInformation'
export default {
  name: 'addInterviewModal',
  components: { BaseInformation, TalkInformation },
  data() {
    return {
      width: 1000,
      visible: false,
      disableSubmit: false,
      confirmLoading: false,
      current: 0,
      steps: [
        {
          title: '基本资料'
        },
        {
          title: '谈话内容'
        },
      ],
      submitLoading: false,
      detailData: undefined,
      // doType: "",
      isRecordAdd: true
    }
  },
  async created() { },
  methods: {
    show(type, record, departmentId) {
      this.visible = true
      this.current = 0
      this.detailData = undefined
      this.isRecordAdd = true
      // this.doType = type
      setTimeout(() => {
        this.$refs.baseInformationId.setDepartmentId(departmentId)
        if (type == 2) {//数据列表进入,读预约详情，数据回显
          // this.$refs.baseInformationId.setData(record)
          this.getBookDetail(record) //获取预约详情
        }
      }, 500)
    },
    //从上面的谈话录入选择人员触发
    onPersonChange(obj) {
      let type = ""
      if (obj.sjskType == 1 || obj.sjskType == 2) {//1,2是社戒社康人员，其他是社会面
        type = '1'//sjsk
      } else {
        type = '2'//社会面
      }
      this.isRecordAdd = false
      this.detailData = undefined
      this.getDetailApi(obj.sjskPersonId, type)
    },
    //从列表的谈话录入选择人员触发
    getBookDetail(record) {
      let id = ""
      let type = ""
      if (record.type) { //待访谈列表改约
        // let id = ""
        if (record.type == 1 || record.type == 2) {//1,2是社戒社康人员，其他是社会面
          type = '1'//sjsk
          id = record.personId
        } else {
          type = '2'//社会面
          id = record.socialId
        }
      } else { //历史记录列表改约
        if (record.personId) {
          type = "1" //sjsk
        } else {
          type = "2" //社会面
        }
      }
      this.isRecordAdd = true
      this.getDetailApi(id, type)
    },
    getDetailApi(id, type) {
      educationTalk.intViewDetails({ id: id, type: type }).then((res) => {
        if (res.success) {
          this.$refs.baseInformationId.setData(res.result, this.isRecordAdd)
          this.detailData = res.result
        } else {
          this.$refs.baseInformationId.setInfo() //读不到预约信息，就设置基本信息
          this.$message.warning(res.message)
        }
      })
        // .catch((e) => {
        //   this.$message.warning(res.message)
        // })
    },
    handleCancel() {
      this.visible = false
    },
    next() {
      // 第一页
      if (this.current == 0) {
        let flag = this.$refs.baseInformationId.onValidate()
        if (!flag) {
          return
        }
        console.log("detailData")
        console.log(this.detailData)
        if (this.detailData == undefined) { 
          this.$refs.talkInformationId.setData(undefined)
        } else {
          this.$refs.talkInformationId.setData(this.detailData) //填充下一页信息

        }
        this.$refs.talkInformationId.getTypeData()
      }
      this.current++
    },
    prev() {
      this.current--
    },
    submitHandler() {
      let flag = this.$refs.talkInformationId.onValidate()
      if (!flag) {
        return
      }
      let baseData = this.$refs.baseInformationId.getData()
      let talkData = this.$refs.talkInformationId.getData()
      // console.log('talkData')
      // console.log(talkData)
      if (talkData != undefined) {
        let params = Object.assign(baseData, talkData)
        console.log('params')
        console.log(params)
        this.submitLoading = true
        educationTalk.intEntry(params).then(res => {
          if (res.success) {
            this.visible = false
            this.submitLoading = false
            this.$message.success(res.message)
            this.$emit('ok')
          } else {
            this.submitLoading = false
            this.$message.warning(res.message)
          }
        })
          .catch(() => {
            this.submitLoading = false
            this.$message.error(res.message)
          })
      }

    }
  }
}
</script>
<style lang="less" scoped>
.con-content {
  .con-content-top {
    width: 60%;
    margin: 10px auto 50px auto;
  }

  // padding-bottom: 50px;
}

.footerBox {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  padding-top: 20px;
  border-top: 1px solid #ccc;
}
</style>
